$(document).ready(function(){
	// >>登录功能开始########################
	var user = new Helper.TextRemark({
		renderId:"i_username",
		remarkText:"用户名/邮箱"
	});
	Helper.doms.enterSub("i_password",function(){
		$("#i_login_a").click();
	});
	$("#i_login_a").click(function(){
		var uname = user.getValue();
		var upass = $("#i_password").val();
		var vcode = "WLGC";
		Helper.login.login(uname,upass,vcode,function(){
			memberQuery.send();
			new Helper.RemoteQuery( {
				url : Helper.basePath + "/refresh.htm",
				onSuccess : function(html) {
					$("#head_member_member_quick_info_ul").remove();
					$("#site-nav").prepend(html);
					$("#head_member_member_quick_info_ul").hide(200);
					$("#head_member_member_quick_info_ul").show(200);
				}
			}).send();
		},function(json){});
	});
	if($.cookie("memberstore_member_u") && $.cookie("memberstore_member_p")) {
		$("#i_username").val($.cookie("memberstore_member_u"));
		$("#i_password").val($.cookie("memberstore_member_p"));
	}
	// >>登录功能结束########################
	// >>发送下载链接开始#####################
	var appurl_mobile = new Helper.TextRemark({
		renderId:"appurl_mobile",
		remarkText:"输手机号获取链接"
	});
	// 手机校验
	var checkMobile = function() {
		var value=appurl_mobile.getValue();
		var flag = false;
		if(!Helper.isEmpty(value) && value.length >= 11){
			flag = Helper.validata.isMobile(value);
			if(!flag) {
				$("#appurl_tip").show().html("请输入正确的手机号！");
			}
		}else {
			$("#appurl_tip").show().html("请输入正确的手机号！");
		}
		return flag;
	};
	// 发送短信
	var sendVerifyCode = function() {
		if(checkMobile())
		{
			var sendSMS = new Helper.RemoteTrans({
				url : Helper.basePath + "/sendValidSMS.htm",
				onSuccess : function(result) {
					timeInterval = result.returnValue;
					intervalFun = window.setInterval(countDown, 1000);
					$("#appurl_tip").show().html("短信发送成功，请注意查收！");
				},
				onError : function(result, status) {
					$("#appurl_tip").show().html(result.message);
				}
			});
			sendSMS.addParameters("mobile", appurl_mobile.getValue());
			sendSMS.send();
		}
	};
	var timeInterval = 60;
	var intervalFun = null;
	/**
	 * 倒计时
	 */
	var countDown = function() {
		if (timeInterval-- > 0) {
			$("#appurl_send").attr({"disabled":true});
			$("#appurl_tip").show().html("已发送，"+(timeInterval + 1) + "秒后可重发");
		} else {
			clearInterval(intervalFun);
			intervalFun = null;
			$("#appurl_tip").html("").hide();
			$("#appurl_send").removeAttr("disabled");
		}
	};
	var getSendTime = new Helper.RemoteTrans({
		url: Helper.basePath + "/getSendTime.htm",
		onSuccess: function(result){
			timeInterval = result.returnValue;
			intervalFun = window.setInterval(countDown, 1000);
		},
		onError: function(result, status){
		}
	});
	getSendTime.send();
	$("#appurl_send").click(function(){
		sendVerifyCode();
	});
	// >>发送下载链接结束#####################
	// >>中奖播报开始#######################
	var broadcastTimer = null;
	var broadcastOver = false;
	$("#bonus_broadcast").show().wrap("<div style='height:"+$("#bonus_broadcast li:first-child").height()+"px;overflow:hidden;'></div>");
	var broadcastScroll = function(){
		if(!broadcastOver){
			$("#bonus_broadcast").animate({ top: -$("#bonus_broadcast li:first-child").height()},{duration:1500, complete:function(){
				$("#bonus_broadcast li:first-child").insertAfter("#bonus_broadcast li:last-child");
				$("#bonus_broadcast").css("top", "0px");
				broadcastTimer = setTimeout(function(){broadcastScroll();}, 1000);
			}});
		}
	};
	broadcastTimer = setTimeout(function(){broadcastScroll();}, 1000);
	$("#bonus_broadcast").mouseover(function(){
		clearInterval(broadcastTimer);
		broadcastTimer = null;
		broadcastOver = true;
	}).mouseout(function(){
		broadcastTimer = setTimeout(function(){broadcastScroll();}, 1000);
		broadcastOver = false;
	});
	var changeNum = function(obj, setNum, targetNum){
		setTimeout(function(){
			if(setNum <= targetNum){
				$(obj).text(setNum);
				changeNum(obj, ++setNum, targetNum);
			}
		}, setNum * 10);
	};
	$("#bonus_total").children("b").each(function(index){
		changeNum($(this), 0, parseInt($(this).attr("lang")));
	});
	// >>中奖播报结束#######################
	var currentGameId = $("#unite_gameid_ul>li[class='cur']").attr("lang");
	$("#unite_more_a").attr("href",Helper.basePath+"/page/unite/index.jsp?gameId="+currentGameId);
	var remoteQuery = new Helper.RemoteQuery({
		url:Helper.basePath+"/lottery/findUniteListCommend.htm",
		renderId:"unite_data_div",
		maxCacheTimeMM:120000,
		params:{callurl:"/page/index/main/sub/unite_data.jsp",gameId:currentGameId,pageSize:5},
		onSuccess:function(){
			$("#unite_data_index_table>tbody>tr>td[lang='text']>input").each(function(){
				var shengyu = Helper.parseInt($(this).attr("lang"));
				Helper.doms.numberKeyupMinMax($(this)[0],1,shengyu);
			});
			$("#unite_data_index_table>tbody>tr>td>a[name='username']").click(function(){
				var plans = $(this).attr("lang").split(",");
				zhanji.addParameters("betCardNo",plans[0]);
				zhanji.addParameters("gameId",plans[1]);
				zhanji.addParameters("userName",$(this).text());
				zhanji.show();
			});
		}
	});
	
	var newBonusQuery = new Helper.RemoteQuery({
		url:Helper.basePath+"/bonus/findNewsBonus.htm",
		renderId:"newsBonus_div",
		params:{size:30},
		onSuccess:function(){
		newsBonus.start();
	}
	});
	
	var prizeRankQuery = new Helper.RemoteQuery({
		url:Helper.basePath+"/bonus/findPrizeRank.htm",
		renderId:"newsBonus_div",
		params:{backurl:"/page/index/main/sub/prizeRank_data.jsp",size:10}
	});
	
	$("#unite_gameid_ul>li").mouseover(function(){
		$("#unite_gameid_ul>li").attr("class","");
		$(this).attr("class","cur");
		var gameId = $(this).attr("lang");
		$("#unite_more_a").attr("href",Helper.basePath+"/page/unite/index.jsp?gameId="+gameId);
		remoteQuery.addParameters("gameId",gameId);
		remoteQuery.send();
	});
	
	var uniteWindow = new Helper.Window( {
		id:"lot_uniteWindow",
		url : Helper.basePath+"/page/index/main/sub/unite_window.jsp",
		title:"参与合买确认",
		width:280,
		height:210
	});
	
	var uniteTrans = new Helper.RemoteTrans({
		url:Helper.basePath+"/member_operate/joinUnite.htm",
		onSuccess:function(data){
			location.href=Helper.basePath+"/member_operate/lotSuccess.htm?type=join&id="+data.returnValue.id;
		},
		onError:function(data){
			Helper.lotterdom.lotteryError(data,this);
		}
	});
	
	var zhanji = new Helper.Window({
		title:"用户历史战绩",
		width:600,
		height:361,
		url:Helper.basePath+"/page/unite/standings.jsp"
	});
	
	var fnUniteLot = function(gameId,planId,count,lotPay){
		uniteTrans.addParameters("planId",planId);
		uniteTrans.addParameters("count",count);
		uniteTrans.addParameters("lotPay",lotPay);
		uniteTrans.addParameters("gameId",gameId);
		uniteTrans.send();
		uniteWindow.close();
	};
	
	var fnShowUniteWindow = function(gameId,planId,fenshu,partMoney) {
		uniteWindow.addParameters("gameId",gameId);
		uniteWindow.addParameters("planId",planId);
		uniteWindow.addParameters("fenshu",fenshu);
		uniteWindow.addParameters("partMoney",partMoney);
		uniteWindow.show();
	};
	
	var memberQuery = new Helper.RemoteQuery( {
		renderId:"user_info_data_div",
		url : Helper.basePath + "/page/index/main/sub/user_info_data.jsp",
		onSuccess:function(){
			if($("#index_isLogin_hidden").val()==="true") {
				$("#user_nologin_div").hide();
				$("#user_info_data_div").show();
			}else {
				$("#user_info_data_div").hide();
				$("#user_nologin_div").show();
			}
		}
	});
	
	
	var quickQuery = new Helper.RemoteQuery({
		url:Helper.basePath+"/main/quickLotteryIssue.htm",
		renderId:"quick_lot_data_div",
		maxCacheTimeMM:120000,
		onSuccess:function(){
			
		}
	});
	
	var _fnQuickQuery = function() {
		var gameId = $("#quick_lot_ul>li[class='cur']").attr("lang");
		var backUrl = "/page/index/main/sub/quick_game/quickLot_"+gameId+".jsp";
		quickQuery.addParameters("backurl",backUrl);
		quickQuery.addParameters("gameId",gameId);
		quickQuery.send();
	};
	
	$("#quick_lot_ul>li[name='game']>a").mouseover(function(){
		var gameId = $("#quick_lot_ul>li[name='game'][class='cur']").attr("lang");
		var currGameId = $(this).parent().attr("lang");
		if(gameId!==currGameId) {
			$("#quick_lot_ul>li[name='game']").attr("class","");
			$(this).parent().attr("class","cur");
			_fnQuickQuery();
		}
	});
	
	var newDrawsQuery = new Helper.RemoteQuery({
		renderId:"news_draw_data_div",
		url:Helper.basePath+"/bonus/findNewsDraws.htm",
		params:{type:"0"},
		maxCacheTimeMM:120000
	});
	
	$("#news_draw_kaijiang>ul>li").mouseover(function(){
		$("#news_draw_kaijiang>ul>li").attr("class","");
		$(this).attr("class","cur");
		var type = $(this).attr("lang");
		newDrawsQuery.addParameters("type",type);
		newDrawsQuery.send();
	});
	
	var newsBonus = new Action.Scroll({
		renderId:"newsBonus_div",
		cls:"newsBonus_clear"
	});
	
	$("#new_bonus_title").mouseover(function(){
		$(this).attr('class','cur');
		$("#prize_rank_title").attr('class','');
		newBonusQuery.send();
	});
	
	$("#prize_rank_title").mouseover(function(){
		$(this).attr('class','cur');
		$("#new_bonus_title").attr('class','');
		prizeRankQuery.send();
	});
	
	newBonusQuery.send();
	memberQuery.send();
	remoteQuery.send();
	newDrawsQuery.send();
	window["main_index"] = {};
	window["main_index"]["fnShowUniteWindow"] = fnShowUniteWindow;
	window["main_index"]["fnUniteLot"] = fnUniteLot;
	_fnQuickQuery();
});
